<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<div id="menumanage_tools">
	<a id="menumange_add" href="javascript:void(0)" class="icon-add" title="新增菜单信息"></a>
	<a id="menumange_edit" href="javascript:void(0)" class="icon-edit" title="修改菜单信息"></a>
	<a id="menumange_del" href="javascript:void(0)" class="icon-delete" title="删除菜单信息"></a>
	<a id="menumange_ref_save" href="javascript:void(0)" class="icon-save" title="保存菜单资源关联信息"></a>
</div>
<div data-type="layout" data-defs="fit:true" style="padding: 1px;">
	<div
		data-defs="region:'west',split:true,title:'#springMessage('menuInfo')',border:false,bodyCls:'byr byb',headerCls:'byb byr',tools:'#menumanage_tools'"
		style="width: 260px; padding: 0px;">
		<ul id="treeMenu_menumanage" data-type="tree" style="padding: 2px"
			data-defs="
					method:'post',border:false,
					onClick : getResources,
					url:'${rc.contextPath}/admin/getMenu.do',
					animate:false,dnd:false"></ul>
	</div>
	<div data-defs="region:'center',title:'#springMessage('resourceInfo')',border:false,bodyCls:'byl byr byb',headerCls:'byl byb'">
		<table id="gridResource_menumanage" data-type="grid" title="" style="height: 340px"
			data-defs="
				method:'post',url: '${rc.contextPath}/admin/getMenuResourceInfo.do',idField:'resourceId',
				rownumbers:true, pagination:false, fitColumns:true, fit:true">
			<thead>
				<tr>
					<th data-defs="field:'resourceId',width:80,checkbox:true">ID</th>
					<!-- 资源名称 -->
					<th data-defs="field:'resourceName',width:120">#springMessage("resourceName")</th>
					<!-- 已关联菜单名称 -->
					<th data-defs="field:'menuName',width:120">#springMessage("relMenuName")</th>
					<!-- 资源类型 -->
					<th data-defs="field:'resourceType',formatter:formatterResource,width:0">#springMessage("resourceType")</th>
				</tr>
			</thead>
		</table>
	</div>
	<div data-defs="region:'south',split:false" style="height: 33px; padding: 0px;">
		<div style="margin: 0px 0; padding-top: 3px;" align="center">
			<a href="#" data-type="button" data-defs="iconCls:'icon-add'" onclick="addMenu()">#springMessage("buttonAdd")</a>
			<a href="#" data-type="button" data-defs="iconCls:'icon-save'" onclick="saveMenu()">#springMessage("buttonSave")</a>
			<a href="#" data-type="button" data-defs="iconCls:'icon-delete'" onclick="deleteMenu()">#springMessage("buttonDelete")</a>
			<a href="#" data-type="button" data-defs="iconCls:'icon-ok','width':160" onclick="saveMenuResource()">#springMessage("buttonSaveMenuResourceRel")</a>
		</div>
	</div>
	<div id="menumange_add_window" data-type="window" title="#springMessage('menuMaintain')"
		data-defs="modal:true,closed:true,iconCls:'icon-save'" style="width: 400px; height: 150px; padding: 1px;">
		<form id="menuForm_menumanage" method="post">
			<!-- 菜单主键 -->
			<input type="hidden" name="menuId"></input>
			<!-- 叶子节点标志 预留字段暂时没用-->
			<input type="hidden" name="leafFlag" value="0"></input>
			<!-- 根节点标志 预留字段暂时没用-->
			<input type="hidden" name="rootFlag" value="0"></input>
			<table class="stable" cellpadding="5" cellspacing="1" style="width: 100%">
				<tr>
					<!-- 菜单父节点 -->
					<td width="40%" class="stdlabel">#springMessage("menuParentId")</td>
					<td width="60%" class="stdinput">
						<input data-type="combotree" type="text" name="menuParentId"
							data-defs="method:'post',url: '${rc.contextPath}/admin/getMenu.do',required:false"></input>
					</td>
				</tr>
				<tr>
					<!-- 菜单名称 -->
					<td width="40%" class="stdlabel">#springMessage("menuName")</td>
					<td width="60%" class="stdinput">
						<input data-type="inputbox" type="text" name="menuName" data-defs="required:true"></input>
					</td>
				</tr>
				<tr>
					<!-- 排序号 -->
					<td width="40%" class="stdlabel">#springMessage("orderNum")</td>
					<td width="60%" class="stdinput">
						<input data-type="numberbox" type="text" name="orderNum" data-defs="required:true"></input>
					</td>
				</tr>
			</table>
		</form>
		<div style="height: auto; padding-top: 5px;" align="center">
			<a id="menumange_clear" href="javascript:void(0)" data-type="button" data-defs="iconCls:'icon-clear'">新增</a>
			<a id="menumange_save" href="#" data-type="button" data-defs="iconCls:'icon-save'">#springMessage("buttonSave")</a>
		</div>
	</div>
</div>
<script type="text/javascript">
	$(document).ready(function() {
	    $("#menumange_clear").bind("click", function() {
		    $("#menuForm_menumanage").form('clear');
	    });
	    $("#menumange_add").bind("click", function() {
		    $("#menuForm_menumanage").form('clear');
		    var options = {
		        "windowId" : "menumange_add_window",
		        "width" : "400",
		        "height" : "170"
		    };
		    openWindow(options);
	    });
	    $("#menumange_edit").bind("click", function() {
		    var menuTreeNodes = $('#treeMenu_menumanage').tree("getSelected");
		    if (menuTreeNodes == null || menuTreeNodes.length == 0) {
			    showMsg("#springMessage('unSelectedMsg')");
			    return;
		    }
		    var options = {
		        "windowId" : "menumange_add_window",
		        "width" : "400",
		        "height" : "170"
		    };
		    openWindow(options);
		    $("#menuForm_menumanage").form('clear');
		    var url = "${rc.contextPath}/admin/getMenuDetail.do?menuId=" + menuTreeNodes.id;
		    $("#menuForm_menumanage").form('load', url);
	    });
	    $("#menumange_del").bind("click", function() {
		    var menuTreeNodes = $('#treeMenu_menumanage').tree("getSelected");
		    if (menuTreeNodes == null || menuTreeNodes.length == 0) {
			    showMsg("#springMessage('unSelectedMsg')");
			    return;
		    }
		    $("#menuForm_menumanage").form('clear');
		    confirmMsg('#springMessage("confirmDelete")', function(r) {
			    if (r) {
				    var url = '${rc.contextPath}/admin/deleteMenu.do?menuId=' + menuTreeNodes.id;
				    submitForm("menuForm_menumanage", url, function(rs) {
					    var returnedObj = $.parseJSON(rs);
					    showMsg(returnedObj.msgInfo);
					    if (returnedObj.msgType == '1') {
						    $("#menuForm_menumanage").form('clear');
					    }
					    $('#treeMenu_menumanage').tree("reload");
				    });
			    }
		    });
	    });
	    $("#menumange_save").bind("click", function() {
		    var url = "${rc.contextPath}/admin/saveMenu.do";
		    var v = $("#menuForm_menumanage").form('validate');
		    if (!v) {
			    return;
		    }
		    submitForm("menuForm_menumanage", url, function(rs) {
			    var returnedObj = $.parseJSON(rs);
			    showMsg(returnedObj.msgInfo);
			    $("#menuForm_menumanage").form('load', {
				    "menuId" : returnedObj.data
			    });
			    $('#treeMenu_menumanage').tree("reload");
		    });
	    });
	    $("#menumange_ref_save").bind("click", function() {
		    var url = '${rc.contextPath}/admin/saveMenuResource.do';
		    var menuTreeNodes = $('#treeMenu_menumanage').tree("getSelected");
		    if (menuTreeNodes == null || menuTreeNodes.length == 0) {
			    showMsg("#springMessage('unSelectedMsg')");
			    return;
		    }
		    var resourceRows = $('#gridResource_menumanage').grid("getChecked");
		    if (resourceRows == null || resourceRows.length == 0) {
			    resourceRows = "";
		    } else {
			    resourceRows = JSON.stringify(resourceRows);
		    }
		    var data = {
		        "menuTreeNodes" : menuTreeNodes.id,
		        "resourceRows" : resourceRows
		    };
		    ajaxPost(url, data, function(rs) {
			    showMsg(rs.msgInfo);
		    });
	    });
	    
    });
    function formatterResource(v, r) {
	    return getComboNameByCode("2", v);
    }
    //根据菜单获取根其关联的资源父节点.
    function getResources(node) {
	    var url = '${rc.contextPath}/admin/getMenuResources.do';
	    var data = {
		    "menuId" : node.id
	    };
	    ajaxPost(url, data, function(rs) {
		    $('#gridResource_menumanage').grid("uncheckAll");
		    if (rs != null) {
			    for ( var i = 0; i < rs.length; i++) {
				    $('#gridResource_menumanage').grid("selectRecord", rs[i].resourceId);
			    }
		    }
	    });
    }
</script>
